package fun.flyee.android.pengshan.acts;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import com.demo.MeetingActivity;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

import cn.bingoogolapple.photopicker.activity.BGAPhotoPickerActivity;
import fun.flyee.android.pengshan.Constant;
import fun.flyee.android.pengshan.Storage;
import fun.flyee.android.pengshan.acts.helper.ServiceHelper;
import fun.flyee.android.pengshan.events.UnauthEvent;
import fun.flyee.android.pengshan.utils.Systems;
import fun.flyee.android.pengshan.widgets.Tips;

public class Router {

    public static void scanQRCode(Activity activity) {
        activity.startActivityForResult(new Intent(activity, QRCodeActivity.class), Constant.INTENT_REQUEST_SCAN_QRCODE);
    }

    public static void login(Context context) {
        context.startActivity(new Intent(context, LoginActivity.class));
    }

    public static void logout(Context context) {
        Storage.del(Storage.TOKEN);
        EventBus.getDefault().post(new UnauthEvent());
        context.startActivity(new Intent(context, LoginActivity.class));
    }

    public static void main(Context context) {
        context.startActivity(new Intent(context, MainActivity.class));
    }

    public static void go(Context context, String path, String query) {
        Intent intent = new Intent(context, FuncActivity.class);
        intent.putExtra("path", path);
        intent.putExtra("query", query);
        context.startActivity(intent);
    }

    public static void jump(Context context, String url) {
        Uri uri = Uri.parse(url);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        context.startActivity(intent);
    }

    public static void forResult(Activity act, String path, String payload) {
        Intent intent = new Intent(act, FuncActivity.class);
        intent.putExtra("path", path);
        intent.putExtra("payload", payload);
        act.startActivityForResult(intent, Constant.INTENT_REQUEST_RESULT);
    }

    public static void withPayload(Activity act, String path, String payload) {
        Intent intent = new Intent(act, FuncActivity.class);
        intent.putExtra("path", path);
        intent.putExtra("payload", payload);
        act.startActivity(intent);
    }

    public static void location(Activity activity) {
        Intent intent = new Intent(activity, FuncActivity.class);
        intent.putExtra("path", "/location/picker");
        intent.putExtra("payload", "");
        activity.startActivityForResult(intent, Constant.INTENT_REQUEST_LOCATION);
    }

    public static void navigate(Context context, Double lat, Double lng) {
        if (lat == 0 || lng == 0) return;
        Uri uri = Uri.parse("geo:" + lat + "," + lng);
        Intent it = new Intent(Intent.ACTION_VIEW, uri);
        ComponentName componentName = it.resolveActivity(context.getPackageManager());
        if (componentName == null) {
            Tips.toast("请先安装地图软件");
        } else {
            context.startActivity(it);
        }
    }

    public static void photo(Activity activity, int max) {
        Intent photoPickerIntent = new BGAPhotoPickerActivity.IntentBuilder(activity)
                .cameraFileDir(new File(activity.getFilesDir(), "tmp"))
                .maxChooseCount(max) // 图片选择张数的最大值
                .selectedPhotos(null) // 当前已选中的图片路径集合
                .pauseOnScroll(false) // 滚动列表时是否暂停加载图片
                .build();

        activity.startActivityForResult(photoPickerIntent, Constant.INTENT_REQUEST_PHOTO_CHOOSE);
    }

    public static void video(Activity activity) {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

        intent.setType("video/*");

        Intent wrapperIntent = Intent.createChooser(intent, null);

        activity.startActivityForResult(wrapperIntent, Constant.INTENT_REQUEST_VIDEO_CHOOSE);
    }

    public static void upload(Activity activity, int max) {
        Intent photoPickerIntent = new BGAPhotoPickerActivity.IntentBuilder(activity)
                .cameraFileDir(new File(activity.getFilesDir(), "tmp"))
                .maxChooseCount(max) // 图片选择张数的最大值
                .selectedPhotos(null) // 当前已选中的图片路径集合
                .pauseOnScroll(false) // 滚动列表时是否暂停加载图片
                .build();

        activity.startActivityForResult(photoPickerIntent, Constant.INTENT_REQUEST_UPLOAD);
    }

    public static void playMonitor(Activity activity, String channel) {
        ServiceHelper.getInstance(activity).playMonitor(channel);
    }

    public static void popupDialog(Context context, int mode, int id, int type, String tip) {
        Systems.wakeUpAndUnlock(context.getApplicationContext());
        Intent intent = new Intent(context, DialogActivity.class);
        intent.putExtra("id", id);
        intent.putExtra("type", type);
        intent.putExtra("mode", mode);
        intent.putExtra("tip", tip);
        context.startActivity(intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP));
    }

    public static void joinConference(Context context, int id, String name) {
        Systems.wakeUpAndUnlock(context.getApplicationContext());
        Intent intent = new Intent(context, ConferenceActivity.class);
        intent.putExtra("meetingid", id);
        intent.putExtra("roomname", name);
        context.startActivity(intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP));
    }

    public static void acceptCall(Context context, int id, int type, String name) {
        Intent intent = new Intent(context, CallActivity.class);
        intent.putExtra("sessionid", id);
        intent.putExtra("calltype", type);
        intent.putExtra("userName", name);
        Systems.wakeUpAndUnlock(context.getApplicationContext());
        context.startActivity(intent
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP));
    }

}
